package com.dankegongyu.lib.dropdownmenu;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;

import com.dankegongyu.lib.R;
import com.dankegongyu.lib.common.util.ScreenUtil;
import com.dankegongyu.lib.common.widget.dropdown.DropDownMenuAdapter;
import com.dankegongyu.lib.common.widget.dropdown.DropDownMenuTabItemView;

import java.util.ArrayList;
import java.util.List;

/**
 * @author WuPuquan
 * @version 1.0
 * @since 2017/9/27 16:25
 */

public class TestDropDownMenuAdapter implements DropDownMenuAdapter {

    private Context mContext;
    private List<DropDownMenuBean> mTabList;
    private List<DropDownMenuTabItemView> mTabViews = new ArrayList<>();

    public TestDropDownMenuAdapter(Context context, List<DropDownMenuBean> list) {
        this.mContext = context;
        this.mTabList = list;

        initTabViews();
    }

    private void initTabViews() {
        mTabViews.clear();
        for (int i = 0; i < mTabList.size(); i++) {
            int drawableNormalResId;
            int drawableSelectedResId;
            boolean isLast = i == getMenuCount() - 1;
            if (isLast) {
                // 最后一个不一样
                drawableNormalResId = R.drawable.ic_clear;
                drawableSelectedResId = R.drawable.ic_clear;
            } else {
                drawableNormalResId = R.drawable.ic_room_filter_tab_arrow_down_normal;
                drawableSelectedResId = R.drawable.ic_room_filter_tab_arrow_down_selected;
            }
            Drawable drawableNormal = mContext.getResources().getDrawable(drawableNormalResId);
            Drawable drawableSelected = mContext.getResources().getDrawable(drawableSelectedResId);
            mTabViews.add(new DropDownMenuTabItemView(mContext)
                    .setText(mTabList.get(i).tabName)
                    .setTabColorNormal(0xff333333)
                    .setTabColorSelected(mContext.getResources().getColor(R.color.colorPrimary))
                    .setDrawableNormal(drawableNormal)
                    .setDrawableSelected(drawableSelected)
                    .setTabGravity(isLast ? Gravity.CENTER_VERTICAL : Gravity.CENTER));
        }
    }

    @Override
    public int getMenuCount() {
        return mTabList == null ? 0 : mTabList.size();
    }

    @Override
    public DropDownMenuTabItemView getTabView(int position) {
        return mTabViews.get(position);
    }

    @Override
    public float getTabWeight(int position) {
        if (position == getMenuCount() - 1) {
            return 1;
        }
        return 2.5f;
    }

    @Override
    public int getBottomMargin(int position) {
        return ScreenUtil.getScreenHeight() / 10;
    }

    @Override
    public View getContentView(int position, FrameLayout parent) {
        return mTabList.get(position).contentView;
    }

    public void setActive(View contentView, boolean active) {
        for (int i = 0; i < mTabList.size(); i++) {
            if (mTabList.get(i).contentView == contentView) {
                mTabViews.get(i).setActive(active);
                break;
            }
        }
    }
}
